//
//  SelectClassCategoryViewController.m
//  huashiedu
//
//  Created by oceans on 2018/9/1.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "SelectClassCategoryViewController.h"
#import "TipManager.h"

@interface SelectClassCategoryViewController () <UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableview;
@property (strong, nonatomic) NSArray *dataArr;
@end

@implementation SelectClassCategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.dataArr = @[@"书法绘画", @"英语口才", @"舞蹈跆拳", @"音乐戏剧", @"国象围棋", @"科学探索", @"学科辅导", @"亲子活动"];
    [self setupView];
}

- (void)setupView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"选择分类";
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(selectDone)];
    [item setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15.0], NSForegroundColorAttributeName: [UIColor colorWithRed:155/255.0 green:155/255.0 blue:155/255.0 alpha:1.0]} forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = item;
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, XNavigationHeight, ScreenWidth, ScreenHeight - XNavigationHeight) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.tableFooterView = [UIView new];
    _tableview.allowsMultipleSelection = YES;
    _tableview.editing = YES;
    [self.view addSubview:_tableview];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identity = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identity];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identity];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *title = _dataArr[indexPath.row];
    cell.textLabel.text = _dataArr[indexPath.row];
    if ([[UserModel shared].loginUser.hobby containsObject:title]) {
        [tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
    }else {
        [tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0] animated:NO];
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

- (void)selectDone {
    
    NSArray *array = [self.tableview indexPathsForSelectedRows];
    NSMutableString *string = [NSMutableString string];
    for (NSIndexPath *index in array) {
        [string appendFormat:@"%ld,", index.row+1];
    }
    __weak typeof(self) weakself = self;
    [[UserModel shared] updateUserinfo:@"hobby" value:string successBlock:^{
        [TipManager showTip:@"修改成功"];
        [weakself.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
